import {useEffect, useState} from 'react';
import {useUpdate} from './useUpdate';

export type RecordItem = {
    tagIds: number[]
    note: string
    category: '+' | '-'
    amount: number
    createdAt: string // ISO 8601
}

type newRecordItem = Omit<RecordItem, 'createdAt'>

export const useRecords = () => {
    const [records, setRecords] = useState<RecordItem[]>([]);
    useEffect( () => {
        setRecords(JSON.parse(window.localStorage.getItem('records') || '[]' ))
    }, [])
    useUpdate( ()=> {
        window.localStorage.setItem('records', JSON.stringify(records))
    }, records)
    const addRecord = (newRecord: newRecordItem) => {
        if (newRecord.amount <= 0) {
            alert('Please type an amount to continue~')
            return false;
        }
        if(newRecord.tagIds.length === 0){
            alert('Please select a tag to continue~')
            return false;
        }
        const record = {...newRecord, createdAt: (new Date()).toISOString()}
        setRecords([...records, record]);
        return true
    };

    return {records, addRecord};
};